package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Chat;
import cn.wolfcode.trip.base.mapper.ChatMapper;
import cn.wolfcode.trip.base.mapper.SystemCallMapper;
import cn.wolfcode.trip.base.query.ChatQuery;
import cn.wolfcode.trip.base.service.IChatService;
import cn.wolfcode.trip.base.util.DateUtil;
import cn.wolfcode.trip.base.util.UserContext;
import cn.wolfcode.trip.base.vo.ChatListVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatServiceImpl implements IChatService {

    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private SystemCallMapper systemCallMapper;

    @Override
    public PageInfo<ChatListVO> query(ChatQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<ChatListVO> list = chatMapper.queryForList(qo);
        for (ChatListVO vo : list) {
            System.out.println(vo);
            System.out.println(vo.getLastSendTime());
            String msg = DateUtil.intervalTimeMsg(vo.getLastSendTime());
            vo.setTimeMsg(msg);//显示多久前
        }
        return new PageInfo(list);
    }

    @Override
    public void updateState(Long id, Integer state) {
        chatMapper.updateStateByChatId(id, state);
    }

    @Override
    public List<Chat> chatHistory(Long sendUserId, Long receiveUserId) {
        System.out.println(UserContext.getCurrentUser());
        List<Chat> list = chatMapper.listChatHistory(sendUserId, receiveUserId);
        for (Chat chat : list) {
            System.out.println(chat.getSendUser());
            if(chat.getSendUser().getId() == UserContext.getCurrentUser().getId()){
                chat.setMine(true);
            }
        }
        chatMapper.updateStateBySUIdAndRUId(sendUserId, receiveUserId);
        return list;
    }
}
